<?php

namespace App\Model;

use Util\Common;

/**
 * 本地合同-模型
 */
class LocalContract extends Model
{

//    use TimeZoneTrait;

    public bool $timestamps = true;

    protected ?string $table = 'local_contract';

    protected array $fillable = [
        'id', 'class_id', 'title', 'title_pic', 'size', 'newstime', 'download', 'tid',
        'png', 'is_del', 'view_count', 'path', 'png_count', 'uid', 'convert_token', 'is_push', 'ext', 'is_finish',
        'is_make', 'special_id', 'banner_special_id'
    ];

    protected array $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
    ];

    /**获取单挑数据
     * @param int $id
     * @return array
     */
    public function getOne(int $id):array{
        return self::where('id',$id)->first(['id','title','png','png_count','class_id'])->toArray();
    }

    /**获取上一篇下一篇
     * @param int $id
     * @return array
     */
    public function getNext(int $id,string $symbol = '>'):array{
        return self::where('id', $symbol,$id )
            ->where('is_del',Common::DEL_SHOW)
            ->orderBy('id','desc')
            ->first(['id','title'])
            ->toArray();
    }

    /**获取列表
     * @param int $page
     * @param int $limit
     * @param string $sort
     * @return array
     */
    public function getList(int $page = 1,int $limit = 20,array $where=[],string $field = 'id',string $sort='desc'):array{
       $where[] = ['is_del',Common::DEL_SHOW];
       $where[] = ['is_finish',Common::IS_FINISH];
        return self::where($where)
            ->orderBy($field, $sort)
            ->offset(($page-1)*$limit)
            ->limit($limit)
            ->get(['id','title','title_pic'])
            ->toArray();
    }


    /**手机端主页列表
     * @param array $class
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function wap_main_list(array $class):array{
        $list = [];
        foreach($class as $k => $v) {
            $class_id = ContractClass::where('label', $v)->value('id');
            if($class_id) {
                $list[$v] = Contract::where('class_id', $class_id)
                    ->limit(10)->orderBy('id', 'desc')->get(['id', 'class_id', 'title', 'title_pic'])->toArray();
            }
        }
        redis()->set(Common::REDIS_WAP_CONTRACT_LIST,json_encode($list));
        return $list;
    }

    /**电脑端主页列表
     * @param array $class_list
     * @return array
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public static function web_main_list(array $class_list):array{
        $list = [];
        foreach ($class_list as $k => $v) {
            if (array_key_exists('children', $v['children'][0])) {
                $list[$v['id']] = Contract::where('class_id', $v['children'][0]['children'][0]['id'])
                    ->limit(10)->orderBy('id', 'desc')->get(['id', 'class_id', 'title', 'title_pic'])->toArray();
            } else {
                $list[$v['id']] = Contract::where('class_id', $v['children'][0]['id'])
               ->limit(10)->orderBy('id', 'desc')->get(['id', 'class_id', 'title', 'title_pic'])->toArray();
            }
        }
        redis()->set(Common::REDIS_WEB_CONTRACT_LIST,json_encode($list));
        return $list;
    }


}
